home *** CD-ROM | disk | FTP | other *** search
- Path: news.uiowa.edu!ozone!maclenna
- From: maclenna@ozone.uiowa.edu (Mark MacLennan)
- Newsgroups: comp.lang.c++
- Subject: Re: How can I include IOSTREAM.H only once?
- Date: 11 Mar 1996 07:33:49 GMT
- Organization: University of Iowa, Iowa City, IA, USA
- Distribution: world
- Message-ID: <4i0l0t$17h0@flood.weeg.uiowa.edu>
- References: <4hsgid$pmm@sam.inforamp.net> <4htg6a$593@news4.digex.net> <4i0fs4$5g9@sam.inforamp.net>
- Reply-To: maclenna@cgrer.uiowa.edu (Mark MacLennan)
- NNTP-Posting-Host: ozone.cgrer.uiowa.edu
-
- In article <4i0fs4$5g9@sam.inforamp.net> rmorin@inforamp.net
- (Randy Charles Morin) writes:
- >In article <4htg6a$593@news4.digex.net>, ell@access1.digex.net (Ell) wrote:
- >>Randy Charles Morin (rmorin@inforamp.net) wrote:
- >>: >#ifndef __STDIO__
- >>: >#include <stdio.h>
- >>: >#endif
- >>
- >>And generally the '#endif' is the last of code in the file.
- >>#ifndef __STDIO__
- >>#include <stdio.h>
- >> /*...code...*/
- >> /*...code...*/
- >>#endif
- >
- >Sorry, but this is wrong. Everybody, including myself is almost always
- >wrong, so let's not belabor the point. What I was attempting to do is include
- >a file only if it has not already been included. Surely the file <stdio.h>
- >has simliar pre-compile statements to what you describe, but the include file
- >will still be loaded during pre-compilation without using the pre-compile
- >statements I describe. My statement load the file only if they have not
- >been pre-loaded, as oppose to the other statements compiling the file only if
- >they have not been previously compiled.
-
- Actually the above example is essentially right and your examples are
- bizzare. To make sure an include file is only included once, no matter
- what, the include file would contain the following pre-processor
- statements:
-
- #ifndef _MY_H_FILE_
- #define _MY_H_FILE_
- /*...code...*/
- /*...code...*/
- #endif
-
- Since the pre-processor will only have to examine the first #ifndef
- statement, very little time is incurred when loading such files -
- so trivial as to be irrelevant to the total compilation time.
- Indeed, the above idiom is very widely used for C++ programs and
- all relevant system include files are usually implemented in this way.
- (although sometimes "#pragma once" is used instead of the above
- but this is not very portable.)
-
- Hey, guess what! You learned something!
-
- - MARK
-
-
- >
- >Example:
- >
- >header.h
- >--------
- >#ifndef HEADER_H
- >#define HEADER_H
- >typedef bool int;
- >#endif
- >------
- >
- >source.cpp
- >----------
- >#ifndef HEADER_H
- >#include "header.h"
- >#endif
- >#ifndef HEADER_H
- >#include "header.h"
- >#endif
- >int main()
- >{
- > bool b;
- > int i=0;
- > b=i;
- > return b;
- >};
- >-----
- >
- >source2.cpp
- >----------
- >#include "header.h"
- >#include "header.h"
- >int main()
- >{
- > bool b;
- > int i=0;
- > b=i;
- > return b;
- >};
- >-----
- >
- >source.cpp will compile faster then source2.cpp because the first source loads
- >the header file once, while the second source loads the header file twice.
- >This is a simply example and no noticeable compile time savings will occur.
- >But if all standard headers used this type of syntax, then compile times would
- >greatly increase.
- >
- >Agrivar
-
-
-